/*
 * Copyright (C) 2012, EADS France
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */
package xowl.interpreter.frontends;

import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author Laurent WOUTERS
 */
public class Tokenizer {
    private static class GrowingString implements java.lang.CharSequence {
        private static final int bufferSize = 1024;
        
        private RewindableStream stream;
        private char[] buffer;
        private int nextEntry;
        
        public int getReadSize() { return nextEntry; }
        
        public GrowingString(RewindableStream stream) {
            this.stream = stream;
            this.buffer = new char[bufferSize];
            this.nextEntry = 0;
        }
        
        @Override public int length() {
            return Integer.MAX_VALUE;
            /*if (stream.isAtEnd())
                return nextEntry;
            return nextEntry + 1;*/
        }

        @Override public char charAt(int index) {
            while (index >= nextEntry) {
                stream.isAtEnd();
                buffer[nextEntry++] = stream.read();
            }
            return buffer[index];
        }

        @Override public CharSequence subSequence(int start, int end) {
            if (start != 0 || end > nextEntry)
                throw new UnsupportedOperationException("Not supported yet.");
            return String.valueOf(buffer, 0, end);
        }
        
        public void reset() { nextEntry = 0; }
    }
    
    private RewindableStream stream;
    private GrowingString string;
    private Map<String, java.util.regex.Pattern> patterns;
    
    public Tokenizer(RewindableStream stream) {
        this.stream = stream;
        this.string = new GrowingString(stream);
        this.patterns = new HashMap<String, java.util.regex.Pattern>();
    }
    
    private java.util.regex.Pattern getPattern(String regexp) {
        java.util.regex.Pattern p = patterns.get(regexp);
        if (p != null)
            return p;
        p = java.util.regex.Pattern.compile(regexp);
        patterns.put(regexp, p);
        return p;
    }
    
    public String matches(String regexp) {
        java.util.regex.Pattern p = getPattern(regexp);
        java.util.regex.Matcher m = p.matcher(string);
        if (m.lookingAt()) {
            String value = m.group();
            stream.rewind(string.getReadSize() - m.end());
            string.reset();
            return value;
        } else {
            stream.rewind(string.getReadSize());
            string.reset();
            return null;
        }
    }
}
